package com.bing.bingPro.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.TOrderMonthDto;
import com.bing.bingPro.controller.vo.TOrderIndexInstallVo;
import com.bing.bingPro.controller.vo.TOrderIndexMoveVo;
import com.bing.bingPro.entity.TOrderIndexBusiMonth;
import com.bing.bingPro.mapper.TOrderIndexBusiMonthMapper;
import com.bing.bingPro.service.TOrderIndexBusiMonthService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 29821
 * @description 针对表【t_order_index_busi_month(工单指标-业务类型-月份)】的数据库操作Service实现
 * @createDate 2023-05-18 15:21:36
 */
@Service
public class TOrderIndexBusiMonthServiceImpl extends ServiceImpl<TOrderIndexBusiMonthMapper, TOrderIndexBusiMonth>
        implements TOrderIndexBusiMonthService {

    @Resource
    private TOrderIndexBusiMonthMapper tOrderIndexBusiMonthMapper;

    @Override
    public List<TOrderIndexInstallVo> queryInstallEquipment(TOrderMonthDto tOrderYearDto) {
        if ("".equals(tOrderYearDto.getProvinceName())) {
            return baseMapper.selectList(Wrappers.lambdaQuery(TOrderIndexBusiMonth.class)
                            .eq(TOrderIndexBusiMonth::getMonth, tOrderYearDto.getMonth().replace("-", ""))
                            .isNull(TOrderIndexBusiMonth::getKmFlag))
                    .stream()
                    .map(gettOrderIndexBusiMonthTOrderIndexInstallVoFunction())
                    .collect(Collectors.toList());
        }
        String provinceName = tOrderYearDto.getProvinceName();
        return baseMapper.selectList(Wrappers.lambdaQuery(TOrderIndexBusiMonth.class)
                        .eq(TOrderIndexBusiMonth::getMonth, tOrderYearDto.getMonth().replace("-", ""))
                        .eq(TOrderIndexBusiMonth::getProvinceName, provinceName)
                        .isNull(TOrderIndexBusiMonth::getKmFlag))
                .stream()
                .map(gettOrderIndexBusiMonthTOrderIndexInstallVoFunction())
                .collect(Collectors.toList());
    }

    private Function<TOrderIndexBusiMonth, TOrderIndexInstallVo> gettOrderIndexBusiMonthTOrderIndexInstallVoFunction() {
        return tOrderIndexBusiMonth -> {
            TOrderIndexInstallVo vo = new TOrderIndexInstallVo();
            vo.setMonth(new StringBuilder(tOrderIndexBusiMonth.getMonth()).insert(4, "-").toString());
            vo.setProvinceName(tOrderIndexBusiMonth.getProvinceName());
            vo.setBusiType(tOrderIndexBusiMonth.getBusiType());
            vo.setInstallFnNum(Optional.ofNullable(tOrderIndexBusiMonth.getInstallFnNum()).map(Object::toString).orElse(null));
            return vo;
        };
    }

    @Override
    public List<TOrderIndexMoveVo> queryMoveEquipment(TOrderMonthDto tOrderYearDto) {
        if ("".equals(tOrderYearDto.getProvinceName())) {
            return baseMapper.selectList(Wrappers.lambdaQuery(TOrderIndexBusiMonth.class)
                            .eq(TOrderIndexBusiMonth::getMonth, tOrderYearDto.getMonth().replace("-", ""))
                            .isNull(TOrderIndexBusiMonth::getKmFlag))
                    .stream()
                    .map(gettOrderIndexBusiMonthTOrderIndexMoveVoFunction())
                    .collect(Collectors.toList());
        }
        String provinceName = tOrderYearDto.getProvinceName();
        return baseMapper.selectList(Wrappers.lambdaQuery(TOrderIndexBusiMonth.class)
                        .eq(TOrderIndexBusiMonth::getMonth, tOrderYearDto.getMonth().replace("-", ""))
                        .eq(TOrderIndexBusiMonth::getProvinceName, provinceName)
                        .isNull(TOrderIndexBusiMonth::getKmFlag))
                .stream()
                .map(gettOrderIndexBusiMonthTOrderIndexMoveVoFunction())
                .collect(Collectors.toList());
    }

    @Override
    public List<TOrderIndexInstallVo> exportInstall(TOrderMonthDto tOrderYearDto) {
        List<TOrderIndexInstallVo> tOrderIndexInstallVos = this.queryInstallEquipment(tOrderYearDto);
        return tOrderIndexInstallVos.stream()
                .map(tOrderIndexInstallVo -> {
                    TOrderIndexInstallVo vo = new TOrderIndexInstallVo();
                    vo.setMonth(tOrderIndexInstallVo.getMonth());
                    vo.setProvinceName(tOrderIndexInstallVo.getProvinceName());
                    vo.setInstallFnNum(tOrderIndexInstallVo.getInstallFnNum());
                    vo.setBusiType(tOrderIndexInstallVo.getBusiType());
                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<TOrderIndexMoveVo> exportMove(TOrderMonthDto tOrderYearDto) {
        List<TOrderIndexMoveVo> tOrderIndexMoveVos = this.queryMoveEquipment(tOrderYearDto);
        return tOrderIndexMoveVos.stream()
                .map(tOrderIndexMoveVo -> {
                    TOrderIndexMoveVo vo = new TOrderIndexMoveVo();
                    vo.setMonth(tOrderIndexMoveVo.getMonth());
                    vo.setProvinceName(tOrderIndexMoveVo.getProvinceName());
                    vo.setMoveFnNum(tOrderIndexMoveVo.getMoveFnNum());
                    vo.setBusiType(tOrderIndexMoveVo.getBusiType());
                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<String> dateList() {
        return tOrderIndexBusiMonthMapper.dateMonth()
                .stream()
                .map(date ->
                        new StringBuilder(date)
                                .insert(4, "-")
                                .toString())
                .collect(Collectors.toList());
    }

    private Function<TOrderIndexBusiMonth, TOrderIndexMoveVo> gettOrderIndexBusiMonthTOrderIndexMoveVoFunction() {
        return tOrderIndexBusiMonth -> {
            TOrderIndexMoveVo vo = new TOrderIndexMoveVo();
            vo.setMonth(new StringBuilder(tOrderIndexBusiMonth.getMonth()).insert(4, "-").toString());
            vo.setProvinceName(tOrderIndexBusiMonth.getProvinceName());
            vo.setBusiType(tOrderIndexBusiMonth.getBusiType());
            vo.setMoveFnNum(Optional.ofNullable(tOrderIndexBusiMonth.getMoveFnNum()).map(Object::toString).orElse(null));
            return vo;
        };
    }
}




